//
//  NSString+YYAuthSDK.m
//  YYSocial
//
//  Created by Cloud on 2018/9/14.
//  Copyright © 2018年 Cloud. All rights reserved.
//

#import "NSString+YYSocialSDK.h"
#import "YYSocialConst.h"

@implementation NSString (YYSocialSDK)
-(NSDictionary *)yy_dictionary{
    NSString *jsonString = self;
    NSData *JSONData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    return [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableLeaves error:nil];
}
-(NSString *)yy_jsonString{
    NSDictionary *dictionary = self;
    NSString * string=[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
    return string;
}
-(NSString *)yy_URLEncodedString{
    NSString *string = self;
    NSString *encodedString = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                                     (CFStringRef)string,
                                                                                                     NULL,
                                                                                                     (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                                     kCFStringEncodingUTF8));
    return encodedString;
}
-(NSString *)yy_URLDecodedString{
    NSString *string = self;
    NSString *decodedString=(__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (__bridge CFStringRef)string, CFSTR(""), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    return decodedString;
}
-(NSString *)yy_ReplaceEmptyString{
    NSString *string = self;
    NSMutableString *mutStr = [NSMutableString stringWithString:string];
    
    NSRange range = {0,string.length};

    [mutStr replaceOccurrencesOfString:@" "withString:@""options:NSLiteralSearch range:range];

    NSRange range2 = {0,mutStr.length};

    [mutStr replaceOccurrencesOfString:@"\n"withString:@""options:NSLiteralSearch range:range2];
    
    return mutStr;
}

-(BOOL)yy_containsString:(NSString *)string{
    if(string == nil) return NO;
    return [self rangeOfString:string].location != NSNotFound;
}


+ (NSDictionary*)getParamsWithUrlString:(NSString*)urlString {
    NSDictionary *result = @{};
    NSRange range = [urlString rangeOfString:YYA_YYClient];
    if (range.location != NSNotFound) {
        urlString = [urlString substringFromIndex:range.length+range.location];
        result = urlString.yy_dictionary;
    }
    return result;
}

@end
